import logging

from fastapi import Request

logger = logging.getLogger(__name__)


async def verify_token(request: Request):
    """登录状态及权限校验依赖项"""
    # from admin.service.system.auth_admin import SystemAuthAdminService
    # from admin.service.system.auth_perm import SystemAuthPermService
    #
    # url = request.url.path.replace('/api/', '')
    # # 路由转权限
    # auths = request.url.path.replace('/api/', '').replace('/', ':')
    #
    # # 免登录接口
    # if url in ConstantConfig.not_login_uri:
    #     return
    #
    # # Token是否为空
    # token = request.headers.get('token', '')
    # if not token:
    #     raise AppException(HttpResp.TOKEN_EMPTY)
    #
    # # Token是否过期
    # token = f'{ConstantConfig.backstage_token_key}{token}'
    # exist_cnt = await RedisUtil.exists(token)
    # if exist_cnt == 0:
    #     raise AppException(HttpResp.TOKEN_INVALID)
    #
    # # 用户信息缓存
    # uid_str = await RedisUtil.get(token)
    # uid = int(uid_str)
    # if not await RedisUtil.hexists(ConstantConfig.backstage_manage_key, uid_str):
    #     await SystemAuthAdminService.cache_admin_user_by_uid(uid, request.state.conn)
    #
    # # 校验用户被删除
    # mapping = json.loads(await RedisUtil.hget(ConstantConfig.backstage_manage_key, uid_str))
    # if not (mapping and mapping.get('is_delete') == 0):
    #     await RedisUtil.delete(token)
    #     await RedisUtil.hdel(ConstantConfig.backstage_manage_key, f'{uid}')
    #     raise AppException(HttpResp.TOKEN_INVALID, msg='用户已删除,请与管理员联系')
    #
    # # 校验用户被禁用
    # if mapping.get('status') == 0:
    #     raise AppException(HttpResp.LOGIN_DISABLE_ERROR)
    #
    # ttl = await RedisUtil.ttl(token)
    # # 令牌剩余30分钟自动续签,如果是永不过期的ttl会返回-1，不存在则返回-2
    # if 0 < ttl < ConstantConfig.token_renew_time:
    #     await RedisUtil.expire(token, ConstantConfig.token_valid_time)
    #     logger.info(f'自动续签{uid}')
    #
    # # 单次请求信息保存
    # request.state.admin_id = uid
    # CURRENT_USER_ID.set(request.state.admin_id)
    # # request.state.role_ids = [int(i) for i in mapping.get('role_ids').split(',')]
    # # 获取mapping字典中的role_ids键对应的值
    # role_ids = mapping.get('role_ids')
    # # 如果role_ids存在
    # if role_ids:
    #     # 如果role_ids是字符串类型将字符串按逗号分隔，转换为列表
    #     if isinstance(role_ids, str):
    #         role_ids = role_ids.split(',')
    #     # 如果role_ids是整数类型将转换为列表
    #     elif isinstance(role_ids, int):
    #         role_ids = [role_ids]
    #     else:
    #         raise AppException(HttpResp.PARAMS_VALID_ERROR)
    # request.state.role_ids = role_ids
    # request.state.current_user = mapping
    # # request.state.username = mapping.get('username')
    # # request.state.nickname = mapping.get('nickname')
    #
    # # 免权限验证接口
    # if auths in ConstantConfig.not_auth_uri or uid == 1:
    #     return
    #
    # role_ids = request.state.role_ids
    # menus = []
    # # 校验角色权限是否存在
    # for role_id in role_ids:
    #     menus_cache = await RedisUtil.hget(ConstantConfig.backstage_roles_key, str(role_id))
    #     if not await RedisUtil.hexists(ConstantConfig.backstage_roles_key, str(role_id)) or menus_cache == '':
    #         menus_cache = await SystemAuthPermService.cache_role_menus_by_role_id(role_id, request.state.conn)
    #     menus.extend(menus_cache.split(',') if isinstance(menus_cache, str) else menus_cache)
    #
    # # 验证是否有权限操作
    # if not (menus and auths in menus):
    #     raise AppException(HttpResp.NO_PERMISSION)

